package com.tbit.main.controller;


import cn.hutool.core.collection.CollUtil;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.MapConstant;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.exception.BaseException;
import com.tbit.main.h2.pojo.ParkPoint;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.ClientDate;
import com.tbit.main.pojo.ParkPointVert;
import com.tbit.main.pojo.ParkPointVertDebug;
import com.tbit.main.remote.rpc.UQHttpClient;
import com.tbit.main.service.*;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 垂直停车配置
 *
 * @author LMY
 * @create 2021-01-04 09:14
 */
@RestController
@RequestMapping("/vert")
public class ParkPointVertController {
    @Autowired
    private ParkPointVertService parkPointVertService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ParkPointService parkPointService;
    @Autowired
    private GeoreGionService georeGionService;

    /**
     * 通过站点id获取
     *
     * @param parkPointVert
     * @param mapType
     * @return
     */
    @RequestMapping("/getByParkPointId")
    public JsonResult getByParkPointId(String token, ParkPointVert parkPointVert, Integer mapType) {
        Assert.notNull(parkPointVert.getParkPointId(), "ParkPoint.id.notNull");
        Assert.notNull(parkPointVert.getAccountId(), "AccountUser.agentId.notNull");
        if (mapType == null) {
            mapType = MapConstant.MAP_GOOGLE;
        }

        ParkPointVert parkPointVertDB = parkPointVertService.getByParkPointId(parkPointVert.getParkPointId());
        if (parkPointVertDB != null) {
            /**经纬度校准*/
            calibrateService.calPolygon(parkPointVertDB, MapConstant.MAP_ORI, mapType);
        }
        return JsonResult.succ(parkPointVertDB);
    }

    /**
     * 添加
     *
     * @param parkPointVert
     * @return
     */
    @RequestMapping("/insert")
    @OperateLog(module = ModuleEnum.VERT, methods = MethodEnum.VERT_ADD)
    public JsonResult insert(String token, ParkPointVert parkPointVert, Integer mapType) {
        Assert.notNull(parkPointVert.getParkPointId(), "ParkPoint.id.notNull");
        Assert.notNull(parkPointVert.getPoints(), "ParkPointVert.points.notNull");
        Assert.notNull(parkPointVert.getAccountId(), "AccountUser.agentId.notNull");
        if (mapType == null) {
            mapType = MapConstant.MAP_GOOGLE;
        }

        //经纬度校验
        calibrateService.calPolygon(parkPointVert, mapType, MapConstant.MAP_ORI);
        parkPointVertService.insert(parkPointVert);
        return JsonResult.succ();
    }

    /**
     * 修改
     *
     * @param parkPointVert
     * @return
     */
    @RequestMapping("/update")
    @OperateLog(module = ModuleEnum.VERT, methods = MethodEnum.VERT_EDIT)
    public JsonResult update(String token, ParkPointVert parkPointVert, Integer mapType) {
        Assert.notNull(parkPointVert.getParkPointId(), "ParkPoint.id.notNull");
        Assert.notNull(parkPointVert.getPoints(), "ParkPointVert.points.notNull");
        Assert.notNull(parkPointVert.getAccountId(), "AccountUser.agentId.notNull");
        if (mapType == null) {
            mapType = MapConstant.MAP_GOOGLE;
        }

        //经纬度校验
        calibrateService.calPolygon(parkPointVert, mapType, MapConstant.MAP_ORI);
        parkPointVertService.edit(parkPointVert);
        return JsonResult.succ();
    }

    /**
     * 获取垂直停车调试工具
     *
     * @param token
     * @param userCode
     * @return
     */
    @RequestMapping("/getVert")
    public JsonResult getVert(String token, String userCode, Integer type, Integer parkPointId, Double lon, Double lat, Integer mapType) throws Exception {
        Assert.notNull(userCode, "Machine.userCode.notNull");
        if (mapType == null) {
            mapType = MapConstant.MAP_GOOGLE;
        }
        if (type == null) {
            type = 1;
        }

        ClientDate debug = UQHttpClient.getVert(userCode, type, parkPointId, lon, lat);

        if (debug == null) {
            throw new BaseException("Machine.notFined.parkPoint");
        }

        if (type == 1) {
            ParkPointVert parkPointVertDB = parkPointVertService.getByParkPointId(((ParkPointVertDebug) debug.getDate()).getParkPointId());
            if (parkPointVertDB != null) {
                /**校准垂直停车线*/
                calibrateService.calPolygon(parkPointVertDB, MapConstant.MAP_ORI, mapType);
                ((ParkPointVertDebug) debug.getDate()).setVertPointsC(parkPointVertDB.getPointsC());


                /**校准站点*/
                ParkPoint parkPoint = parkPointService.getByParkPointId(((ParkPointVertDebug) debug.getDate()).getParkPointId());
                calibrateService.celibrateParkPointOne(parkPoint, mapType);
                ((ParkPointVertDebug) debug.getDate()).setPoints(parkPoint.getPoints());
                ((ParkPointVertDebug) debug.getDate()).setPointsC(parkPoint.getPointsC());
                ((ParkPointVertDebug) debug.getDate()).setLa(parkPoint.getLa());
                ((ParkPointVertDebug) debug.getDate()).setLo(parkPoint.getLo());
                ((ParkPointVertDebug) debug.getDate()).setLoC(parkPoint.getLoC());
                ((ParkPointVertDebug) debug.getDate()).setLaC(parkPoint.getLaC());

            }
        }
        return JsonResult.succ(debug);
    }

    /**
     * 触发状态
     */
    @RequestMapping("/refresh")
    public JsonResult refresh(String token, String userCode, Integer type) {
        Integer state = UQHttpClient.refresh(userCode, type);
        if (state == 0) {
            throw new BaseException("Call.error");
        }
        return JsonResult.succ();
    }

    /**
     * 通过运营区域id获取
     *
     * @param accountId
     * @param mapType
     * @return
     */
    @RequestMapping("/getByAccountId")
    public JsonResult getByAccountId(String token, Integer accountId, Integer mapType,
                                     @RequestParam(value = "regionId", required = false) List<Integer> regionId) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        if (mapType == null) {
            mapType = MapConstant.MAP_GOOGLE;
        }

        AccountUser accountUserToken = tokenService.getAccount(token);

        if (CollUtil.isEmpty(regionId)) {
            regionId = georeGionService.getByAccountUserId(accountUserToken.getAccountUserId());
        }

        List<ParkPointVert> parkPointVertDB = parkPointVertService.getByAccountId(accountId, regionId);
        if (parkPointVertDB != null) {
            /**经纬度校准*/
            calibrateService.calParkPointVertList(parkPointVertDB, MapConstant.MAP_ORI, mapType);
        }
        return JsonResult.succ(parkPointVertDB);
    }
}
